﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.Linq;
using System.Web.Routing;
using System.Web.Mvc;
using System.IO;
using System.Text;
using System.Collections;

namespace Zosnn.Web
{
    public static class MenuHelper
    {
        private static HttpServerUtilityBase Server = null;
        private static HttpRequestBase Request = null;
        private static UrlHelper Url = null;
        private static RouteValueDictionary RouteDictionary = null;
        public static string Navigator(this HtmlHelper helper)
        {
            Server = helper.ViewContext.RequestContext.HttpContext.Server;
            Request = helper.ViewContext.RequestContext.HttpContext.Request;
            Url = new UrlHelper(helper.ViewContext.RequestContext);
            RouteDictionary = helper.ViewContext.RequestContext.RouteData.Values;
            string xmlPath = Server.MapPath(Url.Content("~/Navigator.xml"));
            XDocument doc = XDocument.Load(xmlPath);
            XElement node = FindNode(doc.Root);
            StringBuilder sb = new StringBuilder();
            Stack s = new Stack();
            while (node != null)
            {
                s.Push(node);
                node = node.Parent;
            }


            if (s.Count > 1)
            {
                //输出breadcrumbs.可以自行修改使之符合你的要求  
                while (s.Count != 0)
                {
                    node = (XElement)s.Pop();
                    if (UrlEqual(node))
                    {
                        //如果是当前的
                        sb.AppendLine(string.Format("<span title=\"{1}\">{0}</span>", node.Attribute("Title").Value, node.Attribute("Description").Value));
                    }
                    else
                    {
                        sb.AppendLine(string.Format("<a href=\"{1}\" title=\"{2}\">{0}</a>", node.Attribute("Title").Value,
                            Url.Action(node.Attribute("Action").Value, node.Attribute("Controller").Value),
                            node.Attribute("Description").Value));
                        sb.AppendLine(" > ");
                    }
                }
            }

            return sb.ToString();
        }

        ///   
        /// 查找当前节点  
        ///   
        /// 当前节点  
        /// 找到返回，找不到为空  
        private static XElement FindNode(XElement e)
        {
            XElement result = e;


            if (UrlEqual(e))
            {
                return e;
            }
            else
            {
                if (e.HasElements)
                {
                    foreach (XElement ee in e.Elements())
                    {
                        result = FindNode(ee);
                        if (result != null)
                        {
                            return result;
                        }
                    }
                }
                return null;

            }
        }

        ///   
        /// Url是否相等  
        ///   
        /// 节点  
        private static bool UrlEqual(XElement e)
        {
            string url1 = Url.Action(e.Attribute("Action").Value, e.Attribute("Controller").Value).ToLower();
            string url2 = Url.RouteUrl(RouteDictionary).ToLower();
            return url1 == url2;
        }
    }
}